﻿using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web;

namespace Leaf.NetCore.Cache
{
    /// <summary>
    /// 版本：1.0 描述：本地缓存实现类 日期：2018-11-5 17:48:05 创建人：王志海 Copyright (c) 2015-2018 首溯科技有限公司
    /// </summary>
    public class Cache : ICache
    {
        //public static CacheManager Default = new CacheManager();
        //private IMemoryCache _cache = new MemoryCache("Cache");
        //private IMemoryCache cache;

        private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
        //private static ObjectCache cache = MemoryCache.Default;
        //private static System.Web.Caching.Cache cache = HttpRuntime.Cache;

        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        /// <returns></returns>
        public T GetCache<T>(string cacheKey) where T : class
        {
            object result = null;
            if (cacheKey != "" && cache.TryGetValue(cacheKey, out result))
            {
                return (T)result;
            }
            return default(T);
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        /// <param name="cacheValue">对象数据</param>
        public void SetCache<T>(string cacheKey, T cacheValue) where T : class
        {
            cache.Set(cacheKey, cacheValue, DateTime.Now.AddHours(1));
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        /// <param name="cacheValue">对象数据</param>
        /// <param name="expireTime">到期时间</param>
        public void SetCache<T>(string cacheKey, T cacheValue, DateTime? expireTime) where T : class
        {
            cache.Set(cacheKey, cacheValue, expireTime.Value);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        public void RemoveCache(string cacheKey)
        {
            cache.Remove(cacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public void RemoveCache()
        {
            var keys = GetKeys();
            foreach (var item in keys)
            {
                cache.Remove(item);
            }
        }

        /// <summary>
        /// 获取当前缓存列表
        /// </summary>
        /// <returns>字符串</returns>
        public Dictionary<string, object> GetCacheList()
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            var keys = GetKeys();
            foreach (var item in keys)
            {
                result.Add(item, cache.Get(item));
            }
            return result;
        }

        public List<string> GetKeys()
        {
            const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var entries = cache.GetType().GetField("_entries", flags).GetValue(cache);
            var cacheItems = entries as IDictionary;
            var keys = new List<string>();
            if (cacheItems == null) return keys;
            foreach (DictionaryEntry cacheItem in cacheItems)
            {
                keys.Add(cacheItem.Key.ToString());
            }
            return keys;
        }
    }
}